package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiSketch;
import com.hilaver.dzmis.codetable.CtSeasonNumber;
import com.hilaver.dzmis.codetable.CtSketchType;
import com.hilaver.dzmis.codetable.CtCollarType;
import com.hilaver.dzmis.dao.BiSketchDAO;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.service.IEntityService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class BiSketchServiceImpl extends AbstractBaseService implements
		IEntityService {
	
	private CtServiceImpl ctService;
	
	private BiSketchDAO dao;
	
	public BiSketchServiceImpl(){
		this.dao = new BiSketchDAO();
		this.ctService = new CtServiceImpl();
	}
	
	@Override
	public String delete(int id) throws Exception {
		super.delete(BiSketch.class.getName(), id);
		return super.delete(BiSketch.class.getName(), id);
	}

	public String edit(BiSketch BiSketch) throws Exception {
		String operation = null;
		if(BiSketch.getId() != null){
			BiSketch old = (BiSketch)this.dao.get(BiSketch.class.getName(), BiSketch.getId());
			if(BiSketch.getPhoto1Name() == null){
				BiSketch.setPhoto1(old.getPhoto1());
				BiSketch.setPhoto1Name(old.getPhoto1Name());
			}
			if(BiSketch.getPhoto2Name() == null){
				BiSketch.setPhoto2(old.getPhoto2());
				BiSketch.setPhoto2Name(old.getPhoto2Name());
			}
			if(BiSketch.getPhoto3Name() == null){
				BiSketch.setPhoto3(old.getPhoto3());
				BiSketch.setPhoto3Name(old.getPhoto3Name());
			}
			if(BiSketch.getPhoto4Name() == null){
				BiSketch.setPhoto4(old.getPhoto4());
				BiSketch.setPhoto4Name(old.getPhoto4Name());
			}
			this.dao.getSession().evict(old);
			operation = "02";
		}else{
			operation = "01";
		}
		this.dao.saveOrUpdate(BiSketch);
		super.editSysMbox(BiSketch, operation);
		return "{success: true}";
	}
	
	@Override
	public String get(int id) throws Exception {
		BiSketch BiSketch = (BiSketch)this.dao.get(BiSketch.class.getName(), id);
		
		writeFile(realFileDir + BiSketch.getPhoto1Name(), BiSketch.getPhoto1());
		writeFile(realFileDir + BiSketch.getPhoto2Name(), BiSketch.getPhoto2());
		writeFile(realFileDir + BiSketch.getPhoto3Name(), BiSketch.getPhoto3());
		writeFile(realFileDir + BiSketch.getPhoto4Name(), BiSketch.getPhoto4());
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append("<photo1Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(BiSketch.getPhoto1Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(BiSketch.getPhoto1Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo1Name>");
		
		xmlSB.append("<photo2Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(BiSketch.getPhoto2Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(BiSketch.getPhoto2Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo2Name>");
		
		xmlSB.append("<photo3Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(BiSketch.getPhoto3Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(BiSketch.getPhoto3Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo3Name>");
		
		xmlSB.append("<photo4Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(BiSketch.getPhoto4Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(BiSketch.getPhoto4Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo4Name>");
		
		if(BiSketch.getBiFranceCustomer() != null){
			xmlSB.append("<customerId>");
			xmlSB.append(BiSketch.getBiFranceCustomer().getId());
			xmlSB.append("</customerId>");
			
			xmlSB.append("<customerName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(BiSketch.getBiFranceCustomer().getName());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerName>");
			
			xmlSB.append("<customerDepartment>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(BiSketch.getBiFranceCustomer().getDepartment()==null?"":BiSketch.getBiFranceCustomer().getDepartment());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerDepartment>");
		}
		xmlSB.append(SimpleObj2XML.toXMLFragment(BiSketch));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAllPagination(int page, String sort, String order,
			String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<BiSketch> objList = this.dao.getAllPagination(BiSketch.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filters);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (BiSketch obj : objList) {
			writeFile(realFileDir + obj.getPhoto1Name(), obj.getPhoto1());
			writeFile(realFileDir + obj.getPhoto2Name(), obj.getPhoto2());
			writeFile(realFileDir + obj.getPhoto3Name(), obj.getPhoto3());
			writeFile(realFileDir + obj.getPhoto4Name(), obj.getPhoto4());
			
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append("<ctSketchType>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtSketchType.class.getName(), obj.getCtSketchType(), locale));			
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctSketchType>");
			
			xmlSB.append("<ctCollarType>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtCollarType.class.getName(), obj.getCtCollarType(), locale));			
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctCollarType>");
			
			xmlSB.append("<photo1Name>");
			xmlSB.append(Constants.XML_CDATA[0]);
			if(obj.getPhoto1Name() == null){
				xmlSB.append(NO_IMAGE_NAME);
			}else{
				xmlSB.append(obj.getPhoto1Name());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</photo1Name>");
			
			xmlSB.append("<photo2Name>");
			xmlSB.append(Constants.XML_CDATA[0]);
			if(obj.getPhoto2Name() == null){
				xmlSB.append(NO_IMAGE_NAME);
			}else{
				xmlSB.append(obj.getPhoto2Name());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</photo2Name>");
			
			xmlSB.append("<photo3Name>");
			xmlSB.append(Constants.XML_CDATA[0]);
			if(obj.getPhoto3Name() == null){
				xmlSB.append(NO_IMAGE_NAME);
			}else{
				xmlSB.append(obj.getPhoto3Name());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</photo3Name>");
			
			xmlSB.append("<photo4Name>");
			xmlSB.append(Constants.XML_CDATA[0]);
			if(obj.getPhoto4Name() == null){
				xmlSB.append(NO_IMAGE_NAME);
			}else{
				xmlSB.append(obj.getPhoto4Name());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</photo4Name>");
			
			if(!StringUtils.isEmpty(obj.getCtSeasonNumber())){
				xmlSB.append("<ctSeasonNumber>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(this.ctService.getDescription(CtSeasonNumber.class.getName(), obj.getCtSeasonNumber(), locale));
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</ctSeasonNumber>");
			}
			
			if(obj.getBiFranceCustomer() != null){
				xmlSB.append("<" + COLUMN_CUSTOMER_NAME + ">");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFranceCustomer().getName());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</" + COLUMN_CUSTOMER_NAME + ">");
				
				xmlSB.append("<" + COLUMN_CUSTOEMR_DEPARTMENT + ">");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFranceCustomer().getDepartment()==null?"":obj.getBiFranceCustomer().getDepartment());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</" + COLUMN_CUSTOEMR_DEPARTMENT + ">");
			}
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(BiSketch.class.getName(),
				filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getMaxReferenceIndex(String ctSketchType, String ctCollarType) throws Exception{
		List<Integer> referenceIndexList = this.dao.getMaxReferenceIndex(ctSketchType, ctCollarType);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(Constants.XML_CDATA[0]);
		if(referenceIndexList.size() == 0 || referenceIndexList.get(0) == null){
			xmlSB.append("0001");
		}else{
			Integer index = referenceIndexList.get(0);
			xmlSB.append(StringUtils.to4Digital(++index));
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

}
